from django.db import models
from django.contrib.auth.models import User, Group


# Create your models here.


class Menu(models.Model):
    # 设置菜单栏
    title = models.CharField(max_length=50, unique=True, null=True, verbose_name="菜单标题")
    url = models.CharField(max_length=100, null=True, blank=True, unique=True, verbose_name="菜单url")
    icon = models.CharField(max_length=60, verbose_name="菜单icon")
    note =  models.CharField(max_length=100, unique=True, verbose_name="备注")
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "菜单栏权限表"
        verbose_name_plural = "菜单栏权限表"


class Role(models.Model):
    # 用户角色表
    name = models.CharField(max_length=100, unique=True, verbose_name="角色名")
    
    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "角色表"
        verbose_name_plural = "角色表"

class UserRole(models.Model):
    # 用户与角色绑定
    user = models.ForeignKey(User, related_name='entries', verbose_name='修改用户', on_delete=False)
    role = models.ManyToManyField(to="Role",)

    
    class Meta:
        verbose_name = "用户对应角色表"
        verbose_name_plural = "用户对应角色表"


class Permissions(models.Model):
    # 设置权限，url对应的 增删改查
    title = models.CharField(max_length=100, unique=True, verbose_name="标题")
    url = models.CharField(max_length=100, null=True, blank=True, unique=True, verbose_name="接口url")
    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "操作权限表"
        verbose_name_plural = "操作权限表"

class RolePermission(models.Model):
    # 角色权限表
    title = models.CharField(max_length=100, unique=True, verbose_name="标题")
    permissions = models.ManyToManyField(to="Permissions",)
    role = models.OneToOneField(to="Role", on_delete=False)
    def __str__(self):
        return self.title
    class Meta:
        verbose_name = "角色对应权限表"
        verbose_name_plural = "角色对应权限表"

